import 'package:first_flutter_demo/utils/HexColor.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("首页"),
      ),
      body: HomeListContent(),
    );
  }
}

class HomeListContent extends StatelessWidget {
  var iamgeList = <String>[
    "assets/images/image_1.png",
    "assets/images/image_2.png"
  ];
  var contentList = <String>[
    "转朱阁低绮户照无眠不应有恨转朱阁低绮户照无眠不应有恨转朱阁低绮户照无眠不应有恨转朱阁低绮户照无眠不应有恨",
    "人有悲欢离合，月有阴晴圆缺.人有悲欢离合，月有阴晴圆缺.人有悲欢离合，月有阴晴圆缺.人有悲欢离合，月有阴晴圆缺.人有悲欢离合，月有阴晴圆缺."
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(color: Colors.white),
      child: CustomScrollView(
        controller: null,
        slivers: <Widget>[
          SliverPadding(
            padding: EdgeInsets.fromLTRB(16, 10, 16, 10),
            sliver: SliverToBoxAdapter(
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                    child: Text(
                      "党建学习",
                      style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                          color: Colors.black),
                    ),
                    flex: 1,
                  ),
                  Text(
                    "更多",
                    style: TextStyle(
                      fontSize: 14,
                      color: HexColor("#8A939E"),
                    ),
                  ),
                ],
              ),
            ),
          ),
          SliverPadding(
            padding: EdgeInsets.fromLTRB(16, 10, 0, 0),
            sliver: SliverToBoxAdapter(
              child: Container(
                height: 160,
                child: ListView.builder(
                    scrollDirection: Axis.horizontal,
                    itemCount: 6,
                    itemBuilder: (BuildContext context, int index) {
                      return GridViewContnet(iamgeList[index % 2],
                          contentList[index % 2], "2023-07-18");
                    }),
              ),
            ),
          ),
          SliverPadding(
            padding: EdgeInsets.fromLTRB(16, 10, 16, 10),
            sliver: SliverToBoxAdapter(
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                    child: Text(
                      "新闻资讯",
                      style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                          color: Colors.black),
                    ),
                    flex: 1,
                  ),
                  Text(
                    "更多",
                    style: TextStyle(
                      fontSize: 14,
                      color: HexColor("#8A939E"),
                    ),
                  ),
                ],
              ),
            ),
          ),
          SliverList(
            delegate:
                SliverChildBuilderDelegate((BuildContext context, int index) {
              return ListViewContnet(iamgeList[index % 2],
                  contentList[index % 2], "2023-07-18", "新闻资讯");
            }, childCount: 10),
          ),
        ],
      ),
    );
  }
}

class GridViewContnet extends StatelessWidget {
  String url, content, date;

  GridViewContnet(this.url, this.content, this.date);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 144,
      child: Padding(
        padding: EdgeInsets.fromLTRB(0, 0, 16, 0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              width: 144,
              height: 95,
              decoration: BoxDecoration(
                  image:
                      DecorationImage(image: AssetImage(url), fit: BoxFit.fill),
                  borderRadius: BorderRadius.all(Radius.circular(6))),
            ),
            Text(
              content,
              style: TextStyle(
                  fontSize: 16,
                  color: Colors.black,
                  fontWeight: FontWeight.w500),
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
            ),
            Text(
              date,
              style: TextStyle(fontSize: 14, color: HexColor("#8A939E")),
            )
          ],
        ),
      ),
    );
  }
}

class ListViewContnet extends StatelessWidget {
  String url, content, date, type;

  ListViewContnet(this.url, this.content, this.date, this.type);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 106,
      child: Padding(
        padding: EdgeInsets.fromLTRB(16, 0, 16, 10),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
              child: Padding(
                padding: EdgeInsets.fromLTRB(0, 0, 10, 0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Expanded(
                      child: Text(
                        content,
                        style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.w500,
                            color: Colors.black),
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      ),
                      flex: 1,
                    ),
                    Expanded(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Container(
                            width: 52,
                            height: 18,
                            decoration: BoxDecoration(
                                borderRadius:
                                    BorderRadius.all(Radius.circular(2)),
                                color: HexColor("#F6F6F6")),
                            child: Center(
                              child: Text(
                                type,
                                style: TextStyle(
                                    fontSize: 10, color: HexColor("#626C78")),
                              ),
                            ),
                          ),
                          Expanded(
                            child: Text(""),
                            flex: 1,
                          ),
                          Text(
                            date,
                            style: TextStyle(
                                fontSize: 14, color: HexColor("#8A939E")),
                          ),
                        ],
                      ),
                    )
                  ],
                ),
              ),
              flex: 1,
            ),
            Container(
              width: 106,
              height: 76,
              decoration: BoxDecoration(
                  image:
                      DecorationImage(image: AssetImage(url), fit: BoxFit.fill),
                  borderRadius: BorderRadius.all(Radius.circular(6))),
            ),
          ],
        ),
      ),
    );
  }
}
